home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / msg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  3.9 KB  |  208 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: msg.c,v 5.1 1992/11/01 00:01:21 panos Exp $" ;
  8.  
  9. #include <syslog.h>
  10. #include <fcntl.h>
  11.  
  12. #include "sio.h"
  13. #include "str.h"
  14. #include "xlog.h"
  15.  
  16. #include "options.h"
  17.  
  18. #include "defs.h"
  19. #include "state.h"
  20. #include "config.h"
  21.  
  22.  
  23. static struct name_value priorities[] =
  24.     {
  25.         { "WARNING",        LOG_WARNING        },
  26.         { "ERROR",            LOG_ERR            },
  27.         { "CRITICAL",        LOG_CRIT            },
  28.         { "NOTICE",            LOG_NOTICE        },
  29.         { "DEBUG",            LOG_DEBUG        },
  30.         { "INFO",            LOG_INFO            },
  31.         { NULL,                0                    }
  32.     } ;
  33.  
  34.  
  35. extern int line_count ;
  36. extern struct name_value syslog_facilities[] ;
  37.  
  38. void msg() ;
  39.  
  40. #define BUFSIZE                            2048
  41.  
  42. #define DEFAULT_SYSLOG_LEVEL            LOG_INFO
  43.  
  44.  
  45. status_e msg_init()
  46. {
  47.     xlog_h xh ;
  48.     bool_int facility_error = FALSE ;
  49.     int fd = -1 ;
  50.     char *func = "msg_init" ;
  51.  
  52.     if ( debug.on )
  53.         xh = xlog_create( XLOG_FILELOG, program_name, XLOG_NOFLAGS,
  54.                                             "/dev/tty", O_APPEND + O_WRONLY, 0 ) ;
  55.     else
  56.     {
  57.         if ( filelog_option )
  58.             xh = xlog_create( XLOG_FILELOG, program_name,
  59.                         XLOG_PRINT_TIMESTAMP + XLOG_PRINT_ID + XLOG_PRINT_PID,
  60.                             filelog_option_arg, LOG_OPEN_FLAGS, LOG_FILE_MODE ) ;
  61.         else
  62.         {
  63.             int facility = DEFAULT_SYSLOG_FACILITY ;
  64.             
  65.             if ( syslog_option )
  66.             {
  67.                 struct name_value *nvp ;
  68.  
  69.                 nvp = nv_find_value( syslog_facilities, syslog_option_arg ) ;
  70.                 if ( nvp != NULL )
  71.                     facility = nvp->value ;
  72.                 else
  73.                     facility_error = TRUE ;
  74.             }
  75.  
  76.             xh = xlog_create( XLOG_SYSLOG, program_name, XLOG_NOFLAGS,
  77.                                                     facility, DEFAULT_SYSLOG_LEVEL ) ;
  78.         }
  79.     }
  80.  
  81.     if ( xh == NULL )
  82.         return( FAILED ) ;
  83.     
  84.     xlog_control( xh, XLOG_GETFD, &fd ) ;
  85.     if ( fd != -1 )
  86.     {
  87.         if ( fcntl( fd, F_SETFD, 1 ) == -1 )
  88.         {
  89.             xlog_destroy( xh ) ;
  90.             return( FAILED ) ;
  91.         }
  92.     }
  93.     
  94.     ps.rws.program_log = xh ;
  95.  
  96.     if ( facility_error )
  97.         msg( LOG_ERR, func, "Bad syslog facility: %s", syslog_option_arg ) ;
  98.     return( OK ) ;
  99. }
  100.  
  101.  
  102. void msg_suspend()
  103. {
  104.     xlog_control( ps.rws.program_log, XLOG_PREEXEC ) ;
  105. }
  106.  
  107.  
  108. void msg_resume()
  109. {
  110.     xlog_control( ps.rws.program_log, XLOG_POSTEXEC ) ;
  111. }
  112.  
  113.  
  114. /*
  115.  * The size argument is a value-result argument
  116.  */
  117. PRIVATE int prepare_buffer( level, func, buf, size, fmt, ap )
  118.     int level ;
  119.     char *func ;
  120.     char *buf ;
  121.     unsigned size ;
  122.     char *fmt ;
  123.     va_list ap ;
  124. {
  125.     char *bufstart = buf ;
  126.     unsigned bytes_left = size ;
  127.     int cc ;
  128.  
  129.     /*
  130.      * Check if we need to print the level name
  131.      */
  132.     if ( debug.on || filelog_option )
  133.     {
  134.         struct name_value *nvp = nv_find_name( priorities, level ) ;
  135.         char *pri = nvp ? nvp->name : "UNKNOWN" ;
  136.  
  137.         cc = strx_nprint( bufstart, bytes_left, "%s: ", pri ) ;
  138.         bufstart += cc ;
  139.         bytes_left -= cc ;
  140.     }
  141.  
  142.     /*
  143.      * Check if we need to print the function name
  144.      */
  145.     if ( debug.on || level == LOG_CRIT )
  146.     {
  147.         cc = strx_nprint( bufstart, bytes_left, "{%s} ", func ) ;
  148.         bufstart += cc ;
  149.         bytes_left -= cc ;
  150.     }
  151.  
  152.     cc = strx_nprintv( bufstart, bytes_left, fmt, ap ) ;
  153.  
  154.     bytes_left -= cc ;
  155.  
  156.     return( size - bytes_left ) ;
  157. }
  158.  
  159.  
  160. /* VARARGS3 */
  161. void msg( level, func, fmt, va_alist )
  162.     int level ;
  163.     char *func ;
  164.     char *fmt ;
  165.     va_dcl
  166. {
  167.     va_list ap ;
  168.     char buf[ BUFSIZE ] ;
  169.     int len ;
  170.  
  171.     va_start( ap ) ;
  172.     len = prepare_buffer( level, func, buf, sizeof( buf ), fmt, ap ) ;
  173.     va_end( ap ) ;
  174.  
  175.     xlog_write( ps.rws.program_log, buf, len, XLOG_SET_LEVEL, level ) ;
  176. }
  177.  
  178.  
  179. /*
  180.  * Parser message.
  181.  * There are 2 differences from msg():
  182.  *        1) parsemsg() prints the line #
  183.  *        2) parsemsg() does not interpret %m
  184.  */
  185. /* VARARGS3 */
  186. void parsemsg( msg_level, func, fmt, va_alist )
  187.     int msg_level ;
  188.     char *func ;
  189.     char *fmt ;
  190.     va_dcl
  191. {
  192.     va_list ap ;
  193.     char buf[ BUFSIZE ] ;
  194.     int cc ;
  195.     int len ;
  196.  
  197.     va_start( ap ) ;
  198.     len = prepare_buffer( msg_level, func, buf, sizeof( buf ), fmt, ap ) ;
  199.     va_end( ap ) ;
  200.  
  201.     cc = strx_nprint( &buf[ len ], sizeof(buf)-len, " [line=%d]", line_count ) ;
  202.     len += cc ;
  203.  
  204.     xlog_write( ps.rws.program_log, buf, len, 
  205.             XLOG_NO_ERRNO + XLOG_SET_LEVEL, msg_level ) ;
  206. }
  207.  
  208.